package view;

import java.awt.*;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.List;

import javax.swing.*;
import javax.swing.LayoutStyle.*;
import javax.swing.GroupLayout.*;

import controller.*;

import model.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PeliculaBaja extends JPanel {
	private static final long serialVersionUID = 1L;
	private JComboBox<String> peliculaListaBox;
	private List<Pelicula>peliculas=PeliculaController.getPeliculaController().listar();
	private Hashtable<String,Pelicula>peliculasTabla=new Hashtable<String,Pelicula>();

	/**
	 * Create the panel.
	 */
	public PeliculaBaja() {
		setSize(new Dimension(800, 400));
		
		JLabel peliculaCopiaAltaTit = new JLabel("Eliminar Pel\u00EDcula");
		peliculaCopiaAltaTit.setAlignmentX(Component.CENTER_ALIGNMENT);
		peliculaCopiaAltaTit.setHorizontalAlignment(SwingConstants.CENTER);
		peliculaCopiaAltaTit.setHorizontalTextPosition(SwingConstants.CENTER);
		peliculaCopiaAltaTit.setFont(new Font("Tahoma", Font.BOLD, 15));
		
		JLabel peliculaCopiaAltaNombre = new JLabel("Pel\u00EDcula:");
		peliculaCopiaAltaNombre.setHorizontalAlignment(SwingConstants.RIGHT);
		peliculaListaBox = new JComboBox<String>();
		for(Pelicula pelicula : peliculas){
			peliculasTabla.put(pelicula.getNombre(), pelicula);
			peliculaListaBox.addItem(pelicula.getNombre());
			
		}
		
		JButton peliculaCopiaAltaBtn = new JButton("Eliminar");
		peliculaCopiaAltaBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String key=(String)peliculaListaBox.getSelectedItem();
				Pelicula pelicula=peliculasTabla.get(key);
				try{
					PeliculaController.getPeliculaController().eliminar(pelicula);
					peliculaListaBox.removeItem(peliculaListaBox.getSelectedItem());
				}catch(SQLException e){
				}
			}
		});
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(peliculaCopiaAltaNombre, GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(peliculaCopiaAltaBtn)
								.addComponent(peliculaListaBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addContainerGap(398, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(peliculaCopiaAltaTit, GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
							.addContainerGap())))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(peliculaCopiaAltaTit, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
					.addGap(24)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(peliculaListaBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(peliculaCopiaAltaNombre))
					.addGap(18)
					.addComponent(peliculaCopiaAltaBtn)
					.addContainerGap(285, Short.MAX_VALUE))
		);
		setLayout(groupLayout);

	}
}
